<template>
  <div>
    <el-tree
      node-key="id"
      ref="tree"
      :data="list"
      default-expand-all
      check-strictly
      :props="{ label: 'name', chidren: 'chidren' }"
      show-checkbox
    />
    <el-row type="flex" justify="end" height="60">
      <el-button type="primary" @click="hSubmit">确 定</el-button>
      <el-button type="info" @click="$emit('close')">取 消</el-button>
    </el-row>
  </div>
</template>
<script>
import { getPermissionList } from "@/api/permissions";
import { tranDataToTree } from "@/utils/index";
import { getRoleDetail, assignPerm } from "@/api/settings";

export default {
  props: {
    roleId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.loadpermissionsList();
  },

  methods: {
    //  保存分配权限
    async hSubmit() {
      try {
        // 获取选中节点的id值
        const permIds = this.$refs.tree.getCheckedKeys();
        const res = await assignPerm({ id: this.roleId, permIds: permIds });
        console.log("assignPerm", res);
        // 抛出关闭
        this.$emit("close");
        // 保存数据
      } catch (err) {
        console.log("permissions", err);
      }
    },
    // 权限列表
    async loadpermissionsList() {
      try {
        const res = await getPermissionList();
        console.log("permissions", res);
        this.list = tranDataToTree(res.data);
        const info = await getRoleDetail(this.roleId);
        // info.data.permIds: ['604f7e17f900be1850edb154']
        console.log("getRoleDetail", info);
        this.$refs.tree.setCheckedKeys(info.data.permIds);
        // 保存数据
      } catch (err) {
        console.log("permissions", err);
      }
    },
  },
};
</script>
